<template>
  <el-cascader
    :options="data"
    :props="props"
    :value="value"
    style="width: 300px;"
    separator="-"
    @change="changeValue"
  />
</template>
<script>
import { getDeptList } from '@/api/department'
import { transListToTreeData } from '@/utils/index'
export default {
  props: {
    value: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      data: [],
      props: {
        label: 'name',
        value: 'id'
      }
    }
  },
  created() {
    this.getDeptList()
  },
  methods: {
    async getDeptList() {
      this.data = transListToTreeData(await getDeptList(), 0)
    },
    changeValue(value) {
      if (value.length > 0) {
        this.$emit('input', value[value.length - 1])
      } else {
        this.$emit('input', null)
      }
    }
  }
}
</script>
